package com.wutong.erb.controller.auth;

import com.wutong.erb.dto.UserDTO;
import com.wutong.erb.exception.InvalidTokenException;
import com.wutong.erb.exception.PasswordNotMatchException;
import com.wutong.erb.exception.TokenNeedRefreshException;
import com.wutong.erb.exception.UserNotFoundException;
import com.wutong.erb.other.Response;
import com.wutong.erb.service.AuthService;
import com.wutong.erb.service.UserService;
import com.wutong.erb.util.DeviceUtil;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 用户相关接口
 *
 * @author wutong
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/user")
public class UserController {

    private final AuthService authService;
    private final UserService userService;
    private final RedisTemplate<String, String> redisTemplate;

    @PostMapping("/get-user-info")
    public ResponseEntity<?> getUserInfo(HttpServletRequest request) {
        String token = redisTemplate.opsForValue().get("accessToken-" + DeviceUtil.generateDeviceHash(request));
        if (token == null) {
            return Response.failure()
                    .message("获取用户信息失败，请重新登录")
                    .build()
                    .ok();
        }
        UserDTO.UserInfoDTO userInfoDTO = userService.getUserInfo(token);
        if (userInfoDTO == null) {
            return Response.failure()
                    .message("获取用户信息失败，请重新登录")
                    .build()
                    .ok();
        }
        return Response.success()
                .message("获取用户信息成功")
                .data(userInfoDTO)
                .build()
                .ok();
    }

    @PostMapping("/update-user-info")
    public ResponseEntity<?> updateUserInfo(@RequestBody UserDTO.UserUpdateInfoDTO userInfoDTO,
                                             HttpServletRequest request) {
        String token = redisTemplate.opsForValue().get("accessToken-" + DeviceUtil.generateDeviceHash(request));
        if (token == null) {
            return Response.failure()
                    .message("更新用户信息失败，请重新登录")
                    .build()
                    .ok();
        }
        UserDTO.UserInfoDTO newUserInfoDTO = userService.updateUserInfo(userInfoDTO, token);
        if (userInfoDTO == null) {
            return Response.failure()
                    .message("更新用户信息失败，请重新登录")
                    .build()
                    .ok();
        }
        return Response.success()
                .message("更新用户信息成功")
                .data(userInfoDTO)
                .build()
                .ok();
    }

    @PostMapping("/update-password")
    public ResponseEntity<?> updatePassword(HttpServletRequest request ,@RequestBody UserDTO.UserUpdatePwdDTO passwordDTO) {
        String token = redisTemplate.opsForValue().get("accessToken-" + DeviceUtil.generateDeviceHash(request));
        if (token == null) {
            return Response.failure()
                    .message("密码修改失败，请重新登录")
                    .build()
                    .ok();
        }
        try {
            Response response = userService.updaterPassword(passwordDTO, token);
            if (response.isStatus()) {
                return Response.success()
                        .message("密码修改成功")
                        .build()
                        .ok();
            } else {
                return Response.failure()
                        .message(response.getMessage())
                        .build()
                        .ok();
            }
        } catch (PasswordNotMatchException e) {
            return Response.failure()
                    .message("密码修改失败，旧密码输入错误")
                    .build()
                    .ok();
        }
    }
}
